<?php

include "database_functions.inc.php";

function geta_category_id ()
{
  $allcat = sprintf("SELECT cat_id FROM category");
  $rs = query($allcat);
  $found_id = array();
  while ($row = mysql_fetch_object($rs))
    {
      $found_id[] = $row->cat_id;
    }
  return $found_id;
}

function get_category_info ($catid)
{
  $query_name = sprintf("SELECT * FROM category WHERE cat_id = %d", $catid);
  $rs = query($query_name);
  $row = mysql_fetch_object($rs);
  $cat_id = array(
		  'id' => $row->cat_id,
		  'name' => $row->cat_name,
		  'desc' => $row->cat_desc
		  );
  return $cat_id;
}

function get_item_by_category($catid)
{
  $combycat = sprintf("SELECT com_id FROM category_item WHERE cat_id = %d", $catid);
  $rs = query($combycat);
  $found_id = array();
  while ($row = mysql_fetch_object($rs))
    {
      $found_id[] = $row->com_id;
    }

  $found_item = array();
  foreach ($found_id as $item_id)
    {
      $found_item[] = get_item_info($item_id);
    }
  return $found_item;
}

function get_item_info($itemid)
{
  $name_query = sprintf("SELECT * FROM commodity WHERE com_id = %d", $itemid);
  $rs = query($name_query);
  $row = mysql_fetch_object($rs);
  $found = array('item_name' => $row->com_name,
		 'item_price' => $row->com_price,
		 'item_avail' => $row->available,
		 'item_instore' => $row->com_instore,
		 'item_desc' => $row->com_desc
		 );
  return $found;
}
function new_item($item_name, $avail, $item_price, $desc, $in_store)
{
  $ni_query = sprintf("INSERT INTO commodity(com_name, com_price, com_desc, com_instore, available) VALUES ('%s', %d, '%s', %d, %d)", $item_name, $item_price, $desc, $in_store, $avail);
  $rs = query($ni_query);
  
  return 0;
}
  
function fpbn($string)
{
  $find_query = sprintf("SELECT * FROM commodity WHERE com_name LIKE '%%%s%%' OR com_desc LIKE '%%%s%%'", $string, $string);
  $rs = query($find_query);

  $found = array();
  while ($row = mysql_fetch_object($rs)) {
    $found[] = array('item_name' => $row->com_name,
		     'item_price' => $row->com_price
		     );
  }
  return $found;
}
?>